Imports System
Imports System.Xml

''CONVERTED

Public Enum UserReqTypes
	Logon = 1
	Logoff = 2
	ChangePassword = 3
End Enum

Public Enum UserResponseTypes
	LoggedIn = 1
	LoggedOut = 2
	UserNotRecognized = 3
	PasswordIncorrect = 4
	PasswordChanged = 5
	Other = 6
End Enum

Public Class LogonMsg
	Inherits BaseMsg

	'<FIXML v="4.4" s="20040109" r="20030618">
	'      <UserReq UserReqID="gagcpcuser-1178044824265" Username="gagcpcuser" Password="gagcpcuser" UserReqTyp="1">
	'            <Hdr TID="NYMEX" TSub="cpcapi"/>
	'      </UserReq>
	'</FIXML>

	'## Logon response
	'<FIXML v="4.4" s="20040109" r="20030618">
	'      <UserRsp UserReqID="gagcpcuser-1178044824265" Username="gagcpcuser" UserStat="1">
	'            <Hdr SID="NYMEX" SSub="cpcapi"/>
	'      </UserRsp>
	'</FIXML>

	'<FIXML v="4.4" s="20040109" r="20030618">
	'<UserReq UserReqID="UNIQUE-REQID" UserReqTyp="3" Username="USER"
	'Password="********" NewPassword="nnnnnnnn">
	'<Hdr TID="NYMEX" TSub="cpcapi"/>
	'</UserReq>
	'</FIXML>

    'Protected m_username As String
	Protected m_password As String
	Protected m_requesttype As UserReqTypes
	Protected m_newpassword As String
	Protected m_response As UserResponseTypes

#Region "Properties"

    'Public Property UserName() As String
    '	Get
    '		Return m_username
    '	End Get
    '	Set(ByVal Value As String)
    '		m_username = Value
    '	End Set
    'End Property

	Public Property Password() As String
		Get
			Return m_password
		End Get
		Set(ByVal Value As String)
			m_password = Value
		End Set
	End Property

	Public Property NewPassword() As String
		Get
			Return m_newpassword
		End Get
		Set(ByVal Value As String)
			m_newpassword = Value
		End Set
	End Property

	Public Property LogonType() As UserReqTypes
		Get
			Return m_requesttype
		End Get
		Set(ByVal Value As UserReqTypes)
			m_requesttype = Value
			If Value = 3 Then
				ShowXML = True
			End If
		End Set
	End Property

	Public Property RespType() As UserResponseTypes
		Get
			Return m_response
		End Get
		Set(ByVal Value As UserResponseTypes)
			m_response = Value
		End Set
	End Property

	Public ReadOnly Property RequestTypeString() As String
		Get
			Return m_requesttype.GetHashCode.ToString()
		End Get
	End Property
#End Region

	Public Sub New()
		IsCPC = False
		ResponseType = "UserRsp"
		ShowXML = False
	End Sub

	Protected Overrides Function RequestMsg(ByRef XmlW As XmlTextWriter) As Boolean
		XmlW.WriteStartElement("FIXML")
        XmlW.WriteAttributeString("v", "5.0 SP2")
        XmlW.WriteAttributeString("s", "20090815")
        XmlW.WriteAttributeString("xv", "109")
        XmlW.WriteAttributeString("cv", "CME.0001")

		XmlW.WriteStartElement("UserReq")
        XmlW.WriteAttributeString("UserReqID", Globals.UserName & "-" & UID)
        XmlW.WriteAttributeString("Username", Globals.UserName)

		If LogonType <> UserReqTypes.Logoff Then
			XmlW.WriteAttributeString("Password", m_password)
		End If

		If LogonType = UserReqTypes.ChangePassword Then
			XmlW.WriteAttributeString("NewPassword", m_newpassword)
		End If

		XmlW.WriteAttributeString("UserReqTyp", RequestTypeString)

		XmlW.WriteStartElement("Hdr")
        XmlW.WriteAttributeString("TID", "CME")
        XmlW.WriteAttributeString("TSub", "CPAPI")
        XmlW.WriteAttributeString("SID", "1A3M") 'broker specific code
        XmlW.WriteAttributeString("SSub", Globals.UserName)
		XmlW.WriteEndElement()

		XmlW.WriteEndElement()

		XmlW.WriteEndElement()

		Return True
	End Function

	Protected Overrides Sub ResponseParse(ByRef node As XmlNode)

		Try

			m_response = node.Attributes("UserStat").InnerText
			If m_response = UserResponseTypes.Other Or Not node.Attributes("UserStatText") Is Nothing Then
				m_ErrMsg = node.Attributes("UserStatText").InnerText
			End If

            ' need to see if userstat = 2. if so, need to request a new password. 
            ' UserStat="2" UserStatText="Password expired, please change your password and logon again"
            If m_response = UserResponseTypes.LoggedOut Then
                If m_errmsg.StartsWith("Password expired") Then
                    SetNewPassword()

                End If
            End If




            '		End If
            '## Logon response
            '<FIXML v="4.4" s="20040109" r="20030618">
            '      <UserRsp UserReqID="gagcpcuser-1178044824265" Username="gagcpcuser" UserStat="1">
            '            <Hdr SID="NYMEX" SSub="cpcapi"/>
            '      </UserRsp>
            '</FIXML>
            '	Next
        Catch ex As Exception

        End Try

    End Sub

    Private Sub SetNewPassword()
        Dim sql As String
        Dim dbi As dbInterface = New dbInterface()
        Dim currpass As String = ""
        Dim newpass As String = ""
        Dim ts As String
        Dim pos As Integer = 0
        Dim inc As Integer = 0

        sql = "select nymx_login, nymx_pass, nymx_newpass from users where UserId=0"

        dbi.query(sql)

        If dbi.getval("nymx_newpass") <> "" Then
            '            newpass = dbi.getval("nymx_newpass")
            dbi.closeReader()
            dbi.close()
            dbi = Nothing
            Exit Sub
            'Else
            '    currpass = dbi.getval("nymx_pass")

            '    ts = currpass

            '    While ts <> "" And IsNumeric(ts) = False
            '        ts = ts.Substring(1)
            '        pos += 1
            '    End While

            '    inc = Convert.ToInt32(ts)

            '    inc += 1

            '    newpass = currpass.Substring(0, pos) & inc

            '    sql = "update users set "
            '    sql = sql & " nymx_newpass=" & dbi.quote(newpass)
            '    sql = sql & " where UserId=0"

            '    dbi.query(sql)
        End If

        currpass = dbi.getval("nymx_pass")

        ts = currpass

        While ts <> "" And IsNumeric(ts) = False
            ts = ts.Substring(1)
            pos += 1
        End While

        inc = Convert.ToInt32(ts)

        inc += 1

        newpass = currpass.Substring(0, pos) & inc

        sql = "update users set "
        sql = sql & " nymx_newpass=" & dbi.quote(newpass)
        sql = sql & " where UserId=0"

        dbi.query(sql)

        sql = "insert into clearing_trades (RefId, dt_queued, clearing_house, clearing_broker_id, clearing_status, broker_display) values("
        sql = sql & "0, getdate(), 43, 0, 0,1)"

        dbi.query(sql)

        dbi.closeReader()
        dbi.close()
        dbi = Nothing

    End Sub
End Class
